Page({
  data: {
    data: null,
    value: "",
    garBageList: [],
    searchHistory: [],
  },
  onLoad() {
    my.showLoading();
    this.getHistory();
  },
  getHistory() {
    let that = this;
    my.functionContext
      .callFunction({
        name: "getGarbageSearchHistory",
      })
      .then((res) => {
        my.hideLoading();
        if (res.result) {
          that.setData({
            searchHistory: res.result,
          });
        }
      })
      .catch(() => {
        my.hideLoading();
      });
  },
  handleChange(value) {
    this.setData({
      value,
    });
  },
  quickSearch(e) {
    this.handleSearch(e.currentTarget.dataset.info);
  },
  search() {
    const {
      value
    } = this.data;
    this.handleSearch(value);
  },
  async handleSearch(value) {
    let that = this;
    if (value) {
      my.showLoading();
      my.functionContext
        .callFunction({
          name: "getGarbageInfo",
          data: {
            garBageName: value,
          },
        })
        .then((res) => {
          my.hideLoading();
          if (res.result) {
            that.setData({
              garBageList: res.result,
            });
            that.getHistory();
          }
        })
        .catch(() => {
          my.hideLoading();
          my.alert({
            title: "请求失败",
            context: "请求失败，请重试～",
          });
        });
    }
  },
});